package com.itheima.controller;

import com.itheima.pojo.PageBean;
import com.itheima.pojo.Person;
import com.itheima.pojo.Result;
import com.itheima.pojo.TbCourse;
import com.itheima.service.TbCourseService;
import com.itheima.util.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
@Slf4j

public class TbCourseLogin {
    @Autowired
    private TbCourseService tbCourseService;
    @PostMapping("/person")

    public Result login(@RequestBody Person person) {
        //1.找services通过用户名密码登录
        log.info("密码登录emp,{}",person);
        Person e=  tbCourseService.login(person);
        if(e==null){
            return  Result.error("用户名或密码错误");
        }

        //3.根据用户和密码能够成功的查询到对应的员工(可能成功)
        Map<String,Object> map=new HashMap<>();
        map.put("id",e.getId());
        map.put("name",e.getName());
        String s = JwtUtils.generateJwt(map);
        return  Result.success(s);
    }
    @GetMapping("/course")
    public Result selectClazz(String code, String name, String applicablePerson, String subject,
                              @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime createTime,
                              @RequestParam(defaultValue = "1")Integer page,
                              @RequestParam(defaultValue = "10") Integer pageSize
    ){    PageBean pb= tbCourseService.seleteCousePage(code,name,applicablePerson,subject,createTime,page,pageSize);
        return Result.success(pb);

    }
    @PostMapping("/course")
    public  Result insertClazz(@PathVariable TbCourse tbCourse){

        //调用业务层新增功能
        tbCourseService.save(tbCourse);
        //响应
        return Result.success();
    }
    /*批量删除*/
    @DeleteMapping("/course/{ids}")
    public Result deleteCourseById(@PathVariable List<Integer> ids){
        System.out.println( ids);
        tbCourseService.deleteStudentById(ids) ;
        return  Result .success();

    }
    //根据id查询
    @GetMapping("/course/{id}")
    public Result getById(@PathVariable Integer id){
        TbCourse course = tbCourseService.getById(id);
        return Result.success(course);
    }
    //修改员工
    @PutMapping("/course")
    public Result update(@RequestBody TbCourse tbCourse){
        tbCourseService.update(tbCourse);
        return Result.success();
    }
}
